# include "ObjectConnect.h"

# include "Renderer.h"

ObjectConnect::ObjectConnect()
{

}

ObjectConnect::~ObjectConnect()
{

}

const std::string& ObjectConnect::getNextWorld()
{
	return myNextWorld;
}

void ObjectConnect::init( Renderer* renderer)
{
	myRenderer = renderer;
}

void ObjectConnect::load( const std::string& name)
{
	myType = CONNECT_MAP;

	lua_State* stateTmp = lua_open();
	luaL_openlibs( stateTmp);
	luaL_dofile( stateTmp, LUA_OBJECT_ACTION);
	lua_settop( stateTmp, 0);
	lua_getglobal( stateTmp, name.c_str());

	lua_pushnumber( stateTmp, 1);
	lua_gettable( stateTmp, -2);
	myEntity = myRenderer->createEntity( "way", lua_tostring( stateTmp, -1));
	lua_pop( stateTmp, 1);

	mySceneNode = myRenderer->createSceneNode( "wayNode");
	mySceneNode->attachObject( myEntity);
	myEntity->setQueryFlags( ACTION_FLAG);

	lua_pushnumber( stateTmp, 2);
	lua_gettable( stateTmp, -2);
	myEntity->setVisible( lua_toboolean( stateTmp, -1));
	lua_pop( stateTmp, 1);

	float posXTmp, posYTmp, posZTmp;
	
	lua_pushnumber( stateTmp, 3);
	lua_gettable( stateTmp, -2);
	posXTmp = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 1);

	lua_pushnumber( stateTmp, 4);
	lua_gettable( stateTmp, -2);
	posYTmp = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 1);

	lua_pushnumber( stateTmp, 5);
	lua_gettable( stateTmp, -2);
	posZTmp = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 1);

	mySceneNode->setPosition( posXTmp, posYTmp, posZTmp);

	lua_pushnumber( stateTmp, 6);
	lua_gettable( stateTmp, -2);
	myNextWorld = lua_tostring( stateTmp, -1);
	lua_pop( stateTmp, 1);

	lua_close( stateTmp);
}
